﻿namespace linkedin_bot_demo.Common
{
	using System;

	public abstract class Disposable : IDisposable
	{
		private volatile bool _disposed;
        private readonly object _syncRoot = new object();

		public void Dispose()
		{
			if (_disposed)
				return;

            lock (_syncRoot)
			{
				if (_disposed)
					return;

				try { OnDispose(); }
				catch (Exception ex)
				{
					//Logger.Instance.Error("An exception was occured in method Dispose() of class '" + GetType().FullName + "'", ex);
				}
				finally
				{
					_disposed = true;
				}
			}
		}

		public event EventHandler Disposed;

		protected virtual void OnDispose()
		{
			var handler = Disposed;
			if (handler != null)
				handler(this, EventArgs.Empty);
		}

		[System.Diagnostics.DebuggerStepThrough]
		[System.Diagnostics.DebuggerHidden]
		protected bool CheckDisposed()
		{
			return _disposed;
		}

		[System.Diagnostics.DebuggerStepThrough]
		[System.Diagnostics.DebuggerHidden]
		protected void VerifyDisposed()
		{
			if (_disposed)
			{
				throw new ObjectDisposedException(GetType().FullName);
			}
		}
	}
}
